package com.base.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.common.AjaxResult;
import com.base.common.BaseController;
import com.base.common.TableDataInfo;
import com.base.domain.SysMenu;
import com.base.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gxd
 * @since 2025/6/6
 */
@Slf4j
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单信息")
public class SysMenuController extends BaseController {

    @Autowired
    private SysMenuService menuService;

    @ApiOperation("分页列表-菜单列表")
    @PostMapping("/pageList")
    public TableDataInfo pageList(@ApiParam(value = "菜单查询条件") SysMenu menu) {
        startPage(menu.getPageNum(), menu.getPageSize());
        List<SysMenu> menuList = menuService.pageList(menu);
        return getDataTable(menuList);
    }

    @ApiOperation("详情信息-根据Id获取菜单详情信息")
    @ApiImplicitParam(name = "id", value = "菜单Id", required = true, dataType = "String", paramType = "path")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(menuService.getById(id));
    }

    @ApiOperation("添加或修改-菜单信息")
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@ApiParam(value = "菜单实体", required = true) @Valid @RequestBody SysMenu menu) {
        return success(menuService.addOrEdit(menu));
    }

    @ApiOperation("删除-菜单信息（支持批量）")
    @ApiImplicitParam(name = "ids", value = "菜单Id（多个需逗号分隔）", required = true, allowMultiple = true, dataType = "String", paramType = "path")
    @DeleteMapping("/removeById")
    public AjaxResult removeById(@RequestParam("ids") String ids) {
        Assert.notEmpty(ids, "缺少必要参数：请提供要删除的记录ID");
        return toAjax(menuService.remove(Wrappers.lambdaQuery(SysMenu.class).in(SysMenu::getId, ids.split(StrPool.COMMA))));
    }
}
